﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Terminal.Advanced_AEUC_settings_controls.Settings_AUX;

namespace Terminal.Advanced_AEUC_settings_controls.Scripting
{
    /// <summary>
    /// Wait for message script action
    /// </summary>
    class ScrChkStatus : ScrBasicAction
    {
        /// <summary>
        /// Public constructor for the script action
        /// </summary>
        /// <param name="iDispName">Action display text</param>
        /// <param name="iUsageHelp">Usage help and comments text for the action</param>
        /// <param name="iMsgCntr">Script message dispatch object</param>
        public ScrChkStatus(string iDispName, string iUsageHelp)
            : base(iDispName, iUsageHelp)
        {
            usageHelp += "Input params: GenPayloadStruct(payload struct name) + \nstatus field of payload struct will contain last returned message status\n";
        }

        /// <summary>
        /// Perform the action using specified parameters.
        /// </summary>
        /// <param name="paramList">List of the input/output parameters for the action</param>
        /// <returns>True if action is succesful</returns>
        public override bool Do(List<object> paramList)
        {
            if (breakPoint)
                WaitUserInput();

            if (paramList.Count > 1 && paramList[0].GetType() == typeof(ScrRunTimeEnv) && paramList[1].GetType() == typeof(GenPayloadStruct))
            {
                ScrRunTimeEnv rte = (ScrRunTimeEnv)(paramList[0]);
                GenPayloadStruct pld = (GenPayloadStruct)paramList[1];

                bool found = false;
                for (int i = 0; i < pld.structContentsStringRep.Length; i++)
                {
                    if (pld.structContentsStringRep[i].dispName == "status")
                    {
                        found = true;
                        break;
                    }
                }
                if(!found)
                {
                    Settings_Interfaces.IAEUCSetting[] runTVar = new Settings_Interfaces.IAEUCSetting[1];
                    runTVar[0] = new GenPayloadStruct.NewPldItem();
                    runTVar[0].AEUC_DisplayName = "status";
                    runTVar[0].AEUC_Id = 0;

                    GenPayloadStruct tmp = new GenPayloadStruct("tmp", runTVar);

                    pld.AddPayloadItems(tmp.StructContents, tmp.structContentsStringRep);
                }

                pld[pld["status"]] = (uint)rte.rteResult;

                return true;
            }

            return false;
        }

        /// <summary>
        /// Generate C source code for specified parameters
        /// </summary>
        /// <param name="paramlist">List of input/output parameters for the action</param>
        /// <returns>C source string</returns>
        public override string GenerateSourceCode(List<object> paramList)
        {
            string c_source = "Can't generate source - check script file!";


            return c_source;
        }
        /// <summary>
        /// Returns a List containing objects of the parameter list required for the Do function.
        /// </summary>
        /// <returns>List of parameter objects</returns>
        public override List<Type> GetParamList()
        {
            List<Type> paramList = new List<Type>();
            paramList.Add(typeof(ScrRunTimeEnv));
            paramList.Add(typeof(GenPayloadStruct));

            return paramList;
        }

        /// <summary>
        /// Returns a List containing objects of the parameter list required for the GenerateSourceCode function.
        /// </summary>
        /// <returns>List of parameter objects</returns>
        public override List<Type> GetParamListSourceGen()
        {
            List<Type> paramList = new List<Type>();
            paramList.Add(typeof(ScrRunTimeEnv));
            paramList.Add(typeof(GenPayloadStruct));

            return paramList;
        }
    }
}
